/*
 * Spring 2012 TCSS360 Deliverable 3
 * Otis Alexander
 */

package gui;

import java.awt.Color;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.table.AbstractTableModel;

import model.Auction;
import model.CalendarModel;

class MyTableModel extends AbstractTableModel {

	private static final long serialVersionUID = 1L;

	private static final int DAYS_IN_WEEK = 7;

	private static final int SIX_WEEKS_IN_MONTH = 6;

	private String[] columnNames = {"Sunday", "Monday",
			"Tuesday", "Wednesday", "Thursday", "Friday",
			"Saturday"};

	private JPanel[][] data;

	private Date[] dates;

	private Calendar temp_calendar;

	private CalendarModel calendar_model;

	public MyTableModel(Calendar cal,
			CalendarModel cal_model) {
		temp_calendar = cal;
		calendar_model = cal_model;
		dates = new Date[62];
	}

	public int getColumnCount() {
		return columnNames.length;
	}

	public int getRowCount() {
		return data.length;
	}

	@Override
	public String getColumnName(int col) {
		return columnNames[col];
	}

	public Object getValueAt(int row, int col) {
		return data[row][col];
	}

	public void fillDays(final int current_month) {
		JPanel[][] dataArray = new JPanel[6][7];
		int last_day_in_month = 0;
		if (temp_calendar.get(Calendar.MONTH) == current_month) {
			last_day_in_month =
					temp_calendar
							.getActualMaximum(Calendar.DAY_OF_MONTH);
		} else if (temp_calendar.get(Calendar.MONTH) < current_month) {
			temp_calendar.add(Calendar.MONTH, 1);
			last_day_in_month =
					temp_calendar
							.getActualMaximum(Calendar.DAY_OF_MONTH);
			if (current_month == Calendar.JANUARY) {
				temp_calendar.add(Calendar.YEAR, 1);
			}
		} else {
			temp_calendar.add(Calendar.MONTH, -1);
			last_day_in_month =
					temp_calendar
							.getActualMaximum(Calendar.DAY_OF_MONTH);
			if (current_month == Calendar.DECEMBER) {
				temp_calendar.add(Calendar.YEAR, -1);
			}
		}

		temp_calendar
				.set(Calendar.DAY_OF_MONTH,
						temp_calendar
								.getActualMaximum(Calendar.DAY_OF_MONTH));

		int day_of_week =
				temp_calendar.get(Calendar.DAY_OF_WEEK);
		int k = last_day_in_month;

		Date date = null;
		int auctionCount = 0;

		for (int i = SIX_WEEKS_IN_MONTH - 1; i >= 0; i--) {
			for (int j = DAYS_IN_WEEK - 1; j >= 0; j--) {
				if (j + 1 > day_of_week
						&& i == SIX_WEEKS_IN_MONTH - 1) {
					dataArray[i][j] = new JPanel();
					dataArray[i][j]
							.setBackground(Color.WHITE);
				} else if (k > 0) {
					date =
							calendar_model
									.convertDate(current_month
											+ 1
											+ "/"
											+ k
											+ "/"
											+ temp_calendar
													.get(Calendar.YEAR));
					List<Auction> auction_list =
							calendar_model
									.getAuctions(date);
					if (auction_list != null) {
						dataArray[i][j] = new JPanel();
						dataArray[i][j]
								.setLayout(new BoxLayout(
										dataArray[i][j], 3));
						dataArray[i][j]
								.setBackground(Color.WHITE);
						dataArray[i][j].add(new JLabel(
								Integer.toString(k)));
						for (int l = 0; l < auction_list
								.size(); l++) {
							JLabel jp = new JLabel();
							jp.setText("         "
									+ auction_list.get(l)
											.getNPO());
							dataArray[i][j].add(jp);
							dates[auctionCount] =
									auction_list.get(l)
											.getDate();
						}
					} else {
						dataArray[i][j] = new JPanel();
						dataArray[i][j]
								.setBackground(Color.WHITE);
						dataArray[i][j]
								.setLayout(new BoxLayout(
										dataArray[i][j], 1));
						dataArray[i][j].add(new JLabel(
								Integer.toString(k)));
					}
					k--;
				} else {
					dataArray[i][j] = new JPanel();
					dataArray[i][j]
							.setBackground(Color.WHITE);
				}
			}
		}
		data = dataArray;
	}

	@SuppressWarnings("unchecked")
	public Class getColumnClass(int c) {
		return getValueAt(0, c).getClass();
	}

	@Override
	public boolean isCellEditable(int row, int col) {
		if (col < 1) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public void setValueAt(Object value, int row, int col) {
		data[row][col] = (JPanel) value;
		fireTableCellUpdated(row, col);
	}
}
